package com.yidu.hbatsams.action;

import com.yidu.hbatsams.domain.Module;
import com.yidu.hbatsams.domain.Role;
import com.yidu.hbatsams.domain.User;
import com.yidu.hbatsams.domain.UserRole;
import com.yidu.hbatsams.domain.vo.UserRoleVo;
import com.yidu.hbatsams.domain.vo.UserVo;
import com.yidu.hbatsams.service.*;
import com.yidu.hbatsams.utils.FileTools;
import com.yidu.hbatsams.utils.State;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.*;

/*
 * @return
 * @Author 梁海长
 * @Description //用户
 * @Date 10:45 2019/10/27 0027
 * @Param
 **/
@Controller
@RequestMapping("User")
public class UserContorller {


    @Resource
   private UserService userService;
    @Resource
    private RoleService roleService ;
    @Resource
    private ModuleService moduleService;

    @Resource
    private FileTools fileTools ;


    /**findUser
     * layui中table表分页，默认是page=1，limit=10，与pagehaper参数对应，
     * layui中还需要数据的总个数count
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @RequestMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(@RequestParam("page") Integer pageNum, @RequestParam("limit") Integer pageSize , String userName) throws Exception {



        Map<String,Object> map = new HashMap<>();

        List<User> list = userService.findAll(pageNum,pageSize,userName);

        int count = userService.count(userName);


        map.put("data",list);

        map.put("count",count);

        map.put("code", 0);

        map.put("msg", "");//消息

        return map;


    }

    /**
     * 增加数据
     * @param uservo
     * @return
     */
    @RequestMapping("insert")
    @ResponseBody
    public State insert(UserVo uservo, MultipartFile file) {
        //新建一个日历对象
        Calendar cale = Calendar.getInstance();
        try {
            String img = fileTools.saveFile("user/user/"+cale.get(Calendar.YEAR)+"-"+(cale.get(Calendar.MONTH) + 1),file);
            uservo.setUserImg(img);
        } catch (IOException e) {
            e.printStackTrace();
        }



        if(uservo.getUserId()!=null && !"".equals(uservo.getUserId())) {
            userService.update(uservo);
        }else {
            userService.add(uservo);
        }

        State state = new State();
        state.setState(1);
        state.setMsg("操作成功");
        return state ;
    }


    /**
     * 删除数据
     * @param uservo
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public State delete(UserVo uservo) {
        userService.delete(uservo);
        State state = new State();
        state.setState(1);
        state.setMsg("操作成功");
        return state;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @RequestMapping("findId")
    @ResponseBody
    public UserVo findId(String id) {
        UserVo userVo = userService.findById(id);

        return userVo;
    }


    /**
     * 查询账号密码
     * @return
     */
    @RequestMapping("findUser")
    @ResponseBody
    public UserVo findUser(HttpSession session , UserVo userVo) {
        UserVo user = userService.findAllUser(userVo);
        if(user.getUserId()!=null){
            session.setAttribute("user" , user);
        }
        return user;
    }
    /**
     * 查询人员
     * @param id
     * @return
     */
    @RequestMapping("/findClerk")
    @ResponseBody
    public List findClert(String id){
        return userService.findClert(id);
    }

    /**
     * 查询登录人员
     * @param session
     * @return
     */
    @RequestMapping("/findSession")
    @ResponseBody
    public UserVo findSession(HttpSession session){
        UserVo userVo = (UserVo)session.getAttribute("user");
        return userVo ;
    }

    /**
     * 查询是否已经登录
     * @param session
     * @return
     */
    @RequestMapping("/findModByManager")
    @ResponseBody
    public UserVo findModByManager(HttpSession session) {
        List<Module> menu = new ArrayList<>();
        List<Module> button = new ArrayList<>();
        UserVo man = (UserVo) session.getAttribute("user");
        if(man==null){
            man = new UserVo();
            man.setUserId("0");
            man.setUserName("请重新登录");
        }else{
            List<Role> list = roleService.findUserId(man.getUserId());
            for (int i = 0; i < list.size(); i++) {
                List<Module> list1 = moduleService.findRoleId(list.get(i).getRoleId());
                for (int j = 0; j < list1.size() ; j++) {
                    if(list1.get(j).getStandbyOne().equals("0")){
                        menu.add(list1.get(j));
                    }else{
                        button.add(list1.get(j));
                    }
                }
            }
            man.setMenu(menu);
            man.setButton(button);
        }
        return man;
    }
}
